import { Byte } from '../../api/io/Byte';
import { ITimer } from '../../api/timer/ITimer';
import { IAsync } from '../../api/event/IAsync';



/**黄小西属性枚举*/
export const enum E_HuangXiaoxi {
	/**黄小西ID*/
	HUANG_XIAOXI_ID = 0,
	/**好感度等级*/
	FAVOR_LEVEL = 1,
	/**好感度经验*/
	FAVOR_EXP = 2,
	/**枚举数量*/
	EnumCount = 3
}


/**黄小西*/
export class HuangXiaoxi {
	/**事件ID*/
	public static readonly EVENT_ID = 'STRUCT_DATA_HUANG_XIAOXI_EVENT_ID';
	/**整体更新事件ID（部分只需要监听整体变化）*/
	public static readonly TOTAL_EVENT_ID = 'STRUCT_DATA_HUANG_XIAOXI_TOTAL_EVENT_ID';
	/**缓存池*/
	private static POOL: HuangXiaoxi[] = [];

	/**创建入口*/
	public static create(json?: string): HuangXiaoxi {
		const c = this.POOL.pop() || new HuangXiaoxi();
		c._destroyed = false;
		c.resetProperty();
		if (json) {
			c.parse(json);
		}
		return c;
	}


	/**已释放*/
	protected _destroyed = false;
	/**黄小西ID (uint32)*/
	protected _huangXiaoxiId: number;
	/**好感度等级 (uint16)*/
	protected _favorLevel: number;
	/**好感度经验 (uint32)*/
	protected _favorExp: number;


	/**构建函数-私有，防止外部创建*/
	private constructor() {
	}


	/**释放*/
	public destroy(): void {
		if (this._destroyed) {
			return;
		}
		this._destroyed = true;
		this.resetProperty(true);
		if (HuangXiaoxi.POOL.indexOf(this) === -1) {
			HuangXiaoxi.POOL.push(this);
		}
	}

	/**黄小西ID*/
	public get huangXiaoxiId() {
		return this._huangXiaoxiId;
	}


	/**黄小西ID*/
	public set huangXiaoxiId(v: number) {
		this._huangXiaoxiId = v;
	}


	/**好感度等级*/
	public get favorLevel() {
		return this._favorLevel;
	}


	/**好感度等级*/
	public set favorLevel(v: number) {
		this._favorLevel = v;
	}


	/**好感度经验*/
	public get favorExp() {
		return this._favorExp;
	}


	/**好感度经验*/
	public set favorExp(v: number) {
		this._favorExp = v;
	}



	/**通过枚举获取值*/
	public getV(k: E_HuangXiaoxi): any {
		switch (k) {
			case E_HuangXiaoxi.HUANG_XIAOXI_ID: return this._huangXiaoxiId;
			case E_HuangXiaoxi.FAVOR_LEVEL: return this._favorLevel;
			case E_HuangXiaoxi.FAVOR_EXP: return this._favorExp;
			default: return null;
		}
	}


	/**通过枚举设置值*/
	public setV(k: E_HuangXiaoxi, v: any): void {
		switch (k) {
			case E_HuangXiaoxi.HUANG_XIAOXI_ID: this._huangXiaoxiId = v; break;
			case E_HuangXiaoxi.FAVOR_LEVEL: this._favorLevel = v; break;
			case E_HuangXiaoxi.FAVOR_EXP: this._favorExp = v; break;
			default: break;
		}
	}


	/**初始化*/
	public resetProperty(fromDestroy = false): void {
		//黄小西ID
		this._huangXiaoxiId = 0;
		//好感度等级
		this._favorLevel = 0;
		//好感度经验
		this._favorExp = 0;
	}


	/**序列化*/
	public stringify(toString: boolean): string | any {
		const json: any = {};
		if (this._huangXiaoxiId) {
			json.huangXiaoxiId = this._huangXiaoxiId;
		}
		if (this._favorLevel) {
			json.favorLevel = this._favorLevel;
		}
		if (this._favorExp) {
			json.favorExp = this._favorExp;
		}

		return toString ? JSON.stringify(json) : json;
	}


	/**反序列化*/
	public parse(jsonv: string | any): this {
		this.resetProperty();
		if (!jsonv) {
			return this;
		}
		const json = typeof (jsonv) === 'string' ? JSON.parse(jsonv) : jsonv;
		if (json.huangXiaoxiId) {
			this._huangXiaoxiId = json.huangXiaoxiId;
		}
		if (json.favorLevel) {
			this._favorLevel = json.favorLevel;
		}
		if (json.favorExp) {
			this._favorExp = json.favorExp;
		}
		return this;
	}


	/**序列化-二进制*/
	public toBytes(byte: Byte): void {
		if (!byte) {
			return;
		}
		byte.writeUint32(this._huangXiaoxiId);
		byte.writeUint16(this._favorLevel);
		byte.writeUint32(this._favorExp);
	}


	/**反序列化-二进制*/
	public fromBytes(byte: Byte): this {
		this.resetProperty();
		if (!byte) {
			return;
		}
		this._huangXiaoxiId = byte.readUint32();
		this._favorLevel = byte.readUint16();
		this._favorExp = byte.readUint32();
		return this;
	}


	/**克隆*/
	public clone(): HuangXiaoxi {
		const byte = Byte.createFromPool();
		this.toBytes(byte);
		byte.pos = 0;
		const clonev = HuangXiaoxi.create().fromBytes(byte);
		Byte.recoverToPool(byte);
		return clonev;
	}


	/**处理更新*/
	public processUpdate(jsonv: string): void {
		if (!jsonv) {
			return;
		}

		const eventID = HuangXiaoxi.EVENT_ID;
		const us = JSON.parse(jsonv);

		for (let i = 0, len = us.length; i < len;) {
			const idx = us[i];
			//黄小西ID
			if (idx === E_HuangXiaoxi.HUANG_XIAOXI_ID) {
				const oldv = this._huangXiaoxiId;
				this._huangXiaoxiId = us[i + 1];
				IAsync.dispatch(eventID, idx, this._huangXiaoxiId, oldv);
				i += 2;
				continue;
			}
			//好感度等级
			if (idx === E_HuangXiaoxi.FAVOR_LEVEL) {
				const oldv = this._favorLevel;
				this._favorLevel = us[i + 1];
				IAsync.dispatch(eventID, idx, this._favorLevel, oldv);
				i += 2;
				continue;
			}
			//好感度经验
			if (idx === E_HuangXiaoxi.FAVOR_EXP) {
				const oldv = this._favorExp;
				this._favorExp = us[i + 1];
				IAsync.dispatch(eventID, idx, this._favorExp, oldv);
				i += 2;
				continue;
			}

			console.error('[Struct Data] can not find enum in HuangXiaoxi with idx = ' + idx + ', version maybe wrong.')
		}

		//全更事件
		ITimer.callLater(this, this.dispatchTotalUpdate);
	}


	/**比对不一致结果*/
	public getCompareInfo(other: HuangXiaoxi): string {
		if (!other) {
			return null;
		}

		const updates: any[] = [];

		if (this._huangXiaoxiId !== other._huangXiaoxiId) {
			updates.push(E_HuangXiaoxi.HUANG_XIAOXI_ID, this._huangXiaoxiId);
		}
		if (this._favorLevel !== other._favorLevel) {
			updates.push(E_HuangXiaoxi.FAVOR_LEVEL, this._favorLevel);
		}
		if (this._favorExp !== other._favorExp) {
			updates.push(E_HuangXiaoxi.FAVOR_EXP, this._favorExp);
		}

		const jsonv = updates.length === 0 ? null : JSON.stringify(updates);
		return jsonv;
	}


	/**全部更新*/
	protected dispatchTotalUpdate(): void {
		IAsync.dispatch(HuangXiaoxi.TOTAL_EVENT_ID, this.getV(0));
	}

}
